package com.lq.dept.ui;

import com.lq.dept.dao.DeptDao;
import com.lq.dept.dao.impl.DeptDaoImpl;
import com.lq.dept.domain.Dept;

import java.util.List;
import java.util.Scanner;

//负责控制台交互
public class DeptUI {//new DeptUI(xxx)

    private Scanner  scanner;//得到控制输入

    private DeptDao  deptDao=new DeptDaoImpl();//为了执行sql

    //构造函数
    public DeptUI(Scanner scanner) {
        this.scanner = scanner;
    }

    public void addDept(){
        System.out.println("请输入部门编号：");
        int deptno=scanner.nextInt();
        System.out.println("请输入部门名称：");
        String dname=scanner.next();
        System.out.println("请输入部门地址");
        String loc=scanner.next();
        //用户输入信息---封装---Dept对象
        Dept dept=new Dept(deptno,dname,loc);
        //把数据添加到数据库中
        boolean b=deptDao.addDept(dept);
        if(b){
            System.out.println("添加成功!!!");
        }else{
            System.out.println("添加失败!!!");
        }
    }

    public void delDept(){
        System.out.println("请输入要删除的部门编号：");
        int deptno  =scanner.nextInt();
        boolean b=deptDao.delDept(deptno);
        if(b){
            System.out.println("删除成功!!!");
        }else{
            System.out.println("删除失败!!!");
        }
    }

    public void queryDept(){
        System.out.println("请输入要查询的部门编号：");
        int deptno  =scanner.nextInt();
        Dept dept=deptDao.queryDept(deptno);
        if(dept != null){
            System.out.println(dept);
        }else{
            System.out.println("没有这个部门信息~~~");
        }
    }

    public void queryAllDept(){
        List<Dept> dlist=deptDao.queryAllDept();
        for(Dept d:dlist){
            System.out.println(d);
        }
    }

    public void updateDept(){
        System.out.println("请输入部门编号：");
        int deptno=scanner.nextInt();
        System.out.println("请输入部门名称：");
        String dname=scanner.next();
        System.out.println("请输入部门地址");
        String loc=scanner.next();
        //用户输入信息---封装---Dept对象
        Dept dept=new Dept(deptno,dname,loc);
        //把数据添加到数据库中
        boolean b=deptDao.updateDept(dept);
        if(b){
            System.out.println("更新成功!!!");
        }else{
            System.out.println("更新失败!!!");
        }
    }


}
